Object-Oriented Programming

Object-Oriented Programming: Attempt to build programs by modeling the relationships between objects in the real world. Each of these objects can have two characteristics: state and behavior.

Classes and Instances

Making a class

/*
* Code from Eimacs.com, obtained Nov. 13, 2023.
*/
public class Name
{
	private double myHeight;
	private double myWeight;
	private String myHair;
	
	public Name( double height, double weight, String hair )
	{
		myHeight = height;
		myWeight = weight;
		myHair = hair;
	}
	
	public void sleep( int hours )
	{
		for(int i = 0 ; i < hours ; i++)
			System.out.println("Sleeping...");
	}
	public void talk()
	{
		myWeight -= 0.01;
		System.out.println("Talking...");
	}
}
Class: A “blueprint” for how Java should make a certain object.

We describe a class to Java using a class declaration or definition, as follows:

  • public | Modifier keyword that specifies how visible the class and its instances are to the full program.

  • class | Keyword specifying that a class is being defined.

  • Name | The class name to be used in declaring instances of the class. Should begin with a capital letter.

Instance: A single object produced by a class.

Each instance has its own variables which store different values for the variables needed in the class. It is common to start instance variables with the word my as in myHeight or myWeight, for example.

  • private | Modifier keyword that specifies that each instance of the class can only manipulate its own instance variables.

  • double or String etc. | Declare data type for instance variable.

  • myHeight or myWeight etc. | Name the instance variables.

Constructor: Segment of code that Java will execute whenever an instance of the class is made.

The constructor must have the same name as the class. It will not have a return type (not even void). It can have formal parameters listed in parentheses and separated by commas, as shown above.

In many cases, such as the above example, the constructor’s body simply initializes instance variables to the provided values.

Similar to static methods, the name and parameters for a constructor are known as the constructor signature. More than one constructor can exist if they have differing parameters.

Instance Methods: Model the behavior of the class

Using a class

For the “using a class” section, assume a class has been properly created and is the Person class.

The name of a class works as a variable data type.

/*
* Code from Eimacs.com, obtained Nov. 13, 2023.
*/
public static void main( String[] args )
{
	Person eric;
	eric = new Person( 1.57, 67.2, "brown" );
	for( int i = 0 ; i < 5 ; i++) 
		eric.talk();
	eric.sleep( 3 );
	eric.getHair();
}
  • Person | Data type of eric.

  • eric | Name of object of type Person.

  • new Person | Specifies that eric is a new object of type Person.

  • eric.talk() or eric.sleep( 3 ) | Instance methods modeling the behavior of eric.

As with any other variable being declared and initialized, it can all be done in a single step.

Accessor methods are methods that return the values of instance variables. They should be in the format of getVar() if they were an accessor for the variable myVar. In short, they should use get before the variable name.